import API from './../api'

export default {
  namespace: "global",

  state: {
    token: "",
    roles: [],
    username: ''
  },

  effects: {
    *setToken({ payload }, { put }) {
      yield put({ type: "save", payload });
      
      // 在这里请求用户数据，就可以同步的拿到token
      const userInfo = yield API.getUserInfo()

      // 权限用户名存到redux
      yield put({ type: "save", payload: {
        roles: userInfo.roles,
        username: userInfo.username
      } });
    },
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },
};
